trò chơi Cannon Siege miễn phí
12.321 lượt xem;
- MusicController.cs
- Game Controller /
- Scripts /
- Assets /
- project /
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class MusicController : MonoBehaviour {
6 public static MusicController instance;
7
8 public AudioClip background, gameplay, loseSound, winSound;
9
10 [HideInInspector]
11 public AudioSource audioSource;
12
13 void Awake(){
14 CreateInstance ();
15 InitializeVariables ();
16 }
17
18 // Use this for initialization
19 void Start () {
20
21 }
22
23 // Update is called once per frame
24 void Update () {
25
26 }
27
28 void CreateInstance(){
29 if (instance != null) {
30 Destroy (gameObject);
31 } else {
32 instance = this;
33 DontDestroyOnLoad (gameObject);
34 }
35 }
36
37 void InitializeVariables(){
38 audioSource = GetComponent<AudioSource> ();
39 }
40
41
42 public void PlayBgMusic(){
43 if(background){
44 audioSource.clip = background;
45 audioSource.loop = true;
46 audioSource.Play ();
47 }
48 }
49
50 public void GameplaySound(){
51 if(gameplay){
52 audioSource.clip = gameplay;
53 audioSource.loop = true;
54 audioSource.Play ();
55 }
56 }
57
58 public void StopAllSounds(){
59 if(audioSource.isPlaying){
60 audioSource.Stop ();
61 }
62 }
63
64 }
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class MusicController : MonoBehaviour {
6 public static MusicController instance;
7
8 public AudioClip background, gameplay, loseSound, winSound;
9
10 [HideInInspector]
11 public AudioSource audioSource;
12
13 void Awake(){
14 CreateInstance ();
15 InitializeVariables ();
16 }
17
18 // Use this for initialization
19 void Start () {
20
21 }
22
23 // Update is called once per frame
24 void Update () {
25
26 }
27
28 void CreateInstance(){
29 if (instance != null) {
30 Destroy (gameObject);
31 } else {
32 instance = this;
33 DontDestroyOnLoad (gameObject);
34 }
35 }
36
37 void InitializeVariables(){
38 audioSource = GetComponent<AudioSource> ();
39 }
40
41
42 public void PlayBgMusic(){
43 if(background){
44 audioSource.clip = background;
45 audioSource.loop = true;
46 audioSource.Play ();
47 }
48 }
49
50 public void GameplaySound(){
51 if(gameplay){
52 audioSource.clip = gameplay;
53 audioSource.loop = true;
54 audioSource.Play ();
55 }
56 }
57
58 public void StopAllSounds(){
59 if(audioSource.isPlaying){
60 audioSource.Stop ();
61 }
62 }
63
64 }
Use this for initialization
Update is called once per frame